//获取元素
var password1 = document.querySelector("#password1");
var iconfont1 = document.querySelectorAll(".iconfont");
var password2 = document.querySelector("#password2");
var email = document.querySelector("#email");

//注册事件
email.onblur = function () {
  var re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  if (!re.test(this.value)) {
    iconfont1[0].className = "iconfont tored";
    iconfont1[0].innerHTML = "&#10005;" + "邮箱格式输入错误";
  } else {
    iconfont1[0].className = "iconfont togreen";
    iconfont1[0].innerHTML = "&#10003;" + "格式输入正确";
  }
};
//注册事件
password1.onblur = function () {
  if (this.value.length < 6 || this.value.length > 16) {
    iconfont1[1].className = "iconfont tored";
    iconfont1[1].innerHTML = "&#10005;" + "格式输入错误";
  } else {
    iconfont1[1].className = "iconfont togreen";
    iconfont1[1].innerHTML = "&#10003;" + "格式输入正确";
  }
};

password2.onfocus = function () {
  iconfont1[2].className = "iconfont toblue";
  iconfont1[2].innerHTML = "*再次输入密码";
};
password2.onblur = function () {
  if (this.value.length > 0 && this.value != "") {
    if (this.value == password1.value && this.value != "") {
      iconfont1[2].className = "iconfont togreen";
      iconfont1[2].innerHTML = "&#10003;" + "确认成功";
    } else {
      iconfont1[2].className = "iconfont tored";
      iconfont1[2].innerHTML = "&#10005;" + "密码输入错误";
    }
  }
};
//注册按钮
var btn = document.querySelector("#btn");
var username = document.querySelector("#username");
//倒数
var daonum = document.querySelector(".daoshu");
btn.onclick = function () {
  if (
    username.value == "" ||
    email.value == "" ||
    password1.value == "" ||
    password2.value == ""
  ) {
    alert("请将信息填写完整");
  } else {
    var user = {
      username: username.value,
      email: email.value,
      password: password1.value,
    };
    localStorage.setItem("user", JSON.stringify(user));
    var daosum = 5;
    //倒计时函数
    var daojishi = null;
    daonum.style.display = "block";
    //设置倒数的周期定时器
    daojishi = setInterval(function () {
      if (daosum <= 1) {
        clearInterval(daojishi);
        window.location = "../html/login.html";
      }
      daosum--;
      daonum.innerHTML = "注册成功，页面将在" + daosum + "秒后进入登录页...";
    }, 1000);
  }
};

/*
   
修改密码

 */
//修改密码按钮

var resetBtn = document.querySelector("#resetBtn");

var resetUsername = document.querySelector("#resetUsername");
var resetEmail = document.querySelector("#resetEmail");
var resetPassword = document.querySelector("#resetPassword");
resetEmail.onblur = function () {
  var re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  if (!re.test(this.value)) {
    iconfont1[3].className = "iconfont tored";
    iconfont1[3].innerHTML = "&#10005;" + "邮箱格式输入错误";
  } else {
    iconfont1[3].className = "iconfont togreen";
    iconfont1[3].innerHTML = "&#10003;" + "格式输入正确";
  }
};
resetPassword.onblur = function () {
  if (this.value.length < 6 || this.value.length > 16) {
    iconfont1[4].className = "iconfont tored";
    iconfont1[4].innerHTML = "&#10005;" + "格式输入错误";
  } else {
    iconfont1[4].className = "iconfont togreen";
    iconfont1[4].innerHTML = "&#10003;" + "格式输入正确";
  }
};

resetBtn.onclick = function () {
  //去浏览器对象中取用户信息
  var user = JSON.parse(localStorage.getItem("user"));
  if (
    resetUsername.value == "" ||
    resetEmail.value == "" ||
    resetPassword.value == ""
  ) {
    alert("请将信息填写完整");
  } else {
    if (
      resetUsername.value.trim() == user.username &&
      resetEmail.value.trim() == user.email
    ) {
      user.password = resetPassword.value;
      localStorage.setItem("user", JSON.stringify(user));
      var daosum = 5;
      //倒计时函数
      var daojishi = null;
      daonum.style.display = "block";
      //设置倒数的周期定时器
      daojishi = setInterval(function () {
        if (daosum <= 1) {
          clearInterval(daojishi);
          window.location = "../html/login.html";
        }
        daosum--;
        daonum.innerHTML = "修改成功，页面将在" + daosum + "秒后进入登录页...";
      }, 1000);
    } else {
      alert("请检查用户名或邮箱是否正确");
    }
  }
};
